package org.chmodke.proxy.socks.startup;

import org.chmodke.proxy.socks.config.ServerConfig;
import org.chmodke.proxy.socks.server.SocksServerThread;
import org.chmodke.proxy.socks.util.Banner;
import org.chmodke.proxy.socks.util.C;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author kehao
 * @version 1.0
 * @date 2019/3/15 10:32
 */
public class Startup {
    private static final Logger logger = LoggerFactory.getLogger(Startup.class);

    public static void main(String[] args) throws IOException {
        Banner.print();
        java.security.Security.setProperty("networkaddress.cache.ttl", "86400");
        logger.info("\n\tUSing port openSock4 openSock5 user pwd");
        int port = ServerConfig.SERVER_PORT;
        boolean openSock4 = ServerConfig.OPEN_SOCK_4;
        boolean openSock5 = ServerConfig.OPEN_SOCK_5;
        String user = ServerConfig.USER;
        String pwd = ServerConfig.PWD;

        int i = 0;
        if (args.length > i && null != args[i++]) {
            port = Integer.parseInt(args[i].trim());
        }
        if (args.length > i && null != args[i++]) {
            openSock4 = Boolean.parseBoolean(args[i].trim());
        }
        if (args.length > i && null != args[i++]) {
            openSock5 = Boolean.parseBoolean(args[i].trim());
        }
        if (args.length > i && null != args[i++]) {
            user = args[i].trim();
        }
        if (args.length > i && null != args[i++]) {
            pwd = args[i].trim();
        }

        ScheduledExecutorService scheduled = new ScheduledThreadPoolExecutor(1, (r) -> new Thread(r, "STATISTIC"));
        scheduled.scheduleAtFixedRate(C::statistic, 30, 5 * 60, TimeUnit.SECONDS);

        SocksServerThread.startServer(port, openSock4, openSock5, user, pwd);
    }
}